package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{EventTimeSessionWindows, ProcessingTimeSessionWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo4EventTimeSessionWindow {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(1)
    /**
     * java,1678756528000
     * java,1678756529000
     * java,1678756530000
     * java,1678756531000
     * java,1678756532000
     * java,1678756537000
     * java,1678756540000
     * java,1678756550000
     */
    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val eventDS: DataStream[(String, Long)] = linesDS.map(line => {
      val split: Array[String] = line.split(",")
      val word: String = split(0)
      val ts: Long = split(1).toLong
      (word, ts)
    })

    //指定时间字段
    val assDS: DataStream[(String, Long)] = eventDS.assignAscendingTimestamps(_._2)

    /**
     * 会话窗口，一段时间没有数据将前面的数据划分为一个窗口，进行计算，每个key独立计时的
     * ProcessingTimeSessionWindows:处理时间的会话窗口
     * EventTimeSessionWindows：事件时间的会话窗口,
     */
    assDS
      .map(kv => (kv._1, 1))
      .keyBy(_._1)
      .window(EventTimeSessionWindows.withGap(Time.seconds(5)))//只有新的数据过来中间相差的5秒才会计算
      .sum(1)
      .print()

    env.execute()


  }

}
